<template>
  <el-dialog :visible="showEstateDialog" :title="formData.id?'编辑房产':'添加房产'" @close="closeFn">
    <el-form ref="form" label-width="80px" :model="formData" :rules="rules">
      <!-- --------------------------经纪----------------------------- -->
      <el-form-item label="经纪" prop="agent">
        <el-select v-model="formData.agent.name" placeholder="请输入经纪">
          <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id">
            {{ item.name }}
          </el-option>
        </el-select>
      </el-form-item>
      <!-- --------------------------分类----------------------------- -->
      <el-form-item label="分类" prop="category">
        <el-select v-model="formData.category.title" placeholder="请输入分类">
          <el-option v-for="item in typeList" :key="item.id" :label="item.title" :value="item.id">
            {{ item.title }}
          </el-option>
        </el-select>
      </el-form-item>
      <!-- --------------------------标题----------------------------- -->
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入标题" />
      </el-form-item>
      <!-- --------------------------标签----------------------------- -->
      <el-form-item label="标签" prop="tags">
        <el-tag
          v-for="(tag,index) in formData.tags"
          :key="index"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
      </el-form-item>
      <!-- --------------------------地址----------------------------- -->
      <el-form-item label="地址" prop="address">
        <el-row type="flex">
          <el-cascader
            v-model="formData.address"
            :options="addressList"
            placeholder="请选择地址"
            @change="handleChange"
          />
        </el-row>
      </el-form-item>
      <!-- --------------------------价格----------------------------- -->
      <el-form-item label="价格" prop="price">
        <el-input v-model="formData.price" />
      </el-form-item>
      <!-- --------------------------描述(介绍)----------------------- -->
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" />
      </el-form-item>
      <!-- --------------------------内容----------------------------- -->
      <el-form-item label="内容" />
      <!-- --------------------------介绍图片------------------------- -->
      <el-form-item label="介绍图片">
        <UploadImage ref="picture" />
      </el-form-item>
      <!-- --------------------------封面----------------------------- -->
      <el-form-item label="封面">
        <UploadImage ref="cover" />
      </el-form-item>
    </el-form>
    <!-- -----------------------确认 与 取消 按钮-------------------- -->
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </el-row>
    </template>

  </el-dialog>

</template>

<script>
import { addProperty, editorProperty, getSpecifProperty } from '@/api/realEstate'
import { getBrokers } from '@/api/brokers' // 获取经纪人列表
import { getTypes } from '@/api/types' // 获取分类
import address from '@/utils/address.json' // 引入地址详情

export default {
  props: {
    showEstateDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 标签的
      inputVisible: false,
      inputValue: '',

      agentList: [], // 经纪人列表
      typeList: [], // 分类列表

      formData: {
        title: '', // 标题
        desc: '', // 描述
        address: '', // 地址
        price: '', // 价格
        tags: [], // 标签
        category: '', // 分类
        agent: '', // 经纪
        photos: '', // 图片
        image: '' // 封面
      },

      addressList: [], // 地址的

      // 校验
      rules: {
        agent: [
          { required: true, message: '请选择经纪人', trigger: 'change' }
        ],
        category: [
          { required: true, message: '请选择分类', trigger: 'change' }
        ],
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请选择地址', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取经纪人列表
    const res = await getBrokers()
    this.agentList = res.data

    // 获取分类列表
    const result = await getTypes()
    this.typeList = result.data

    // 地址-省市级联
    this.addressList = address.data.map(item => {
      return {
        value: item.province,
        label: item.province,
        children: item.city.map(item => {
          return {
            value: item.city,
            label: item.city,
            children: item.district.map(item => {
              return {
                value: item.district,
                label: item.district
              }
            })
          }
        })
      }
    })
  },
  methods: {
    // 回显图片和封面
    async getSpecifProperty(id) {
      const arr = await getSpecifProperty(id)
      this.formData = arr.data
      if (this.formData.image) {
        this.$refs.cover.fileList.push({
          url: this.formData.image
        })
      } if (this.formData.photos) {
        this.$refs.picture.fileList.push({
          url: this.formData.photos
        })
      }
     
      
    },

    // -----确认按钮-----
    async confirmFn() {
      // 上传-介绍图片
      this.formData.photos = this.$refs.picture.fileList.length && this.$refs.picture.fileList[0].url
      // 上传-封面
      this.formData.image = this.$refs.cover.fileList.length && this.$refs.cover.fileList[0].url

      // 校验
      await this.$refs.form.validate()

      // 新建和编辑房产
      if (this.formData.id) {
        await editorProperty(this.formData)
      } else {
        await addProperty(this.formData)
      }
      this.closeFn()
      this.$emit('updataEvent')// 刷新列表
    },

    // -----关闭弹窗函数-----
    closeFn() {
      this.$emit('update:showEstateDialog', false)
      // 清空校验
      this.$refs.form.resetFields()
      // 清空表单
      this.formData = {
        title: '', // 标题
        desc: '', // 描述
        address: '', // 地址
        price: '', // 价格
        tags: [], // 标签
        category: '', // 分类
        agent: '', // 经纪
        photos: '', // 图片
        image: '' // 封面
      }
      this.$refs.cover.fileList = []
      this.$refs.picture.fileList = []
    },

    // ---地址-数组转换字符串---
    handleChange(value) {
      // console.log(176, this.formData.address)
      // console.log(177, value)
      this.formData.address = value.toString()
      // console.log(178, this.formData.address)
    },

    // -------标签的-------
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        if (!Array.isArray(this.formData.tags)) {
          // 优化
          this.formData.tags = []
        }
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}

</script>

<style scoped>
 .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
